package com.dreamstations.changshads.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.activity.ArticleDetailActivity;
import com.dreamstations.changshads.activity.ArticleDetail_WebActivity;
import com.dreamstations.changshads.adapter.DSRvAdapter;
import com.dreamstations.changshads.entity.DatumLiteratureBean;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.RequestType;
import com.squareup.picasso.Picasso;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;


public class Datum_Simple_Fragment extends BaseFragmentForHome<DatumLiteratureBean> {
    private static final String TAG = Datum_Simple_Fragment.class.getName();

    public Datum_Simple_Fragment() {
    }

    @SuppressLint("ValidFragment")
    public Datum_Simple_Fragment(String title, @RequestType String reqType, int category) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("reqType", reqType);
        bundle.putInt(PARAM_TYPEID, category);
        setArguments(bundle);
    }

    @SuppressWarnings("WrongConstant")
    @NonNull
    @Override
    public DSRequest getDsRequest() {
        Bundle bundle = getArguments();
        return new DSRequest()
                .type(bundle.getString("reqType"))
                .addParams(PARAM_TYPEID, String.valueOf(bundle.getInt(PARAM_TYPEID)))
                .setTag(Datum_Simple_Fragment.this);
    }

    @NonNull
    @Override
    public DSRvAdapter.RvSupport<DatumLiteratureBean> getItem() {
        return new DSRvAdapter.RvSupport<DatumLiteratureBean>() {
            @Override
            public int getItemViewType(DatumLiteratureBean data) {
                if (data.getCover() == null || data.getCover().equals("")) {  //没有封面
                    return 0;
                } else {
                    return 1;
                }
            }

            @Override
            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
                ViewHolder holder;
                if (viewType == 1) {
                    holder = ViewHolder.get(context, null, parent, R.layout.datum_item, -1);
                } else {
                    holder = ViewHolder.get(context, null, parent, R.layout.simple_nocover_item, -1);
                }
                return holder;
            }

            @Override
            public void convert(final ViewHolder holder, DatumLiteratureBean data) {
                holder.setText(R.id.item_title, data.getTitle());
                holder.setText(R.id.item_time, data.getTime());
                if (data.getCover() == null || data.getCover().equals("")) {  //没有封面
                    return ;
                }else {
                    ImageView imageView = holder.getView(R.id.item_cover);
                    Picasso.with(getActivity())
                            .load(DSRequest.SERVER_BASE1 + data.getCover())
                            .error(R.drawable.home_nav_header)
                            .into(imageView);
                }
            }
        };
    }


    @Override
    public OnItemClickListener getOnItemClickListener() {
        return new OnItemClickListener<DatumLiteratureBean>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, DatumLiteratureBean data, int position) {
                Intent intent = new Intent();
                if (data.getTemplate() == 1) {
                    intent.setClass(getActivity(), ArticleDetail_WebActivity.class);
                } else {
                    intent.setClass(getActivity(), ArticleDetailActivity.class);
                }
                intent.putExtra("data", data);
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, DatumLiteratureBean o, int position) {
                return false;
            }
        };
    }

    @Override
    public DSRvAdapter.RvSupport getHeader() {
        return null;
    }

    @Override
    public boolean getEnablePullLoadMore() {
        return true;
    }
}
